from ..common import *

class CollapsibleRootPathManage(wx.Panel):

    def __init__(self, parent):
        wx.Panel.__init__(self, parent)
        self.sizer = wx.BoxSizer(wx.VERTICAL)
        self.SetSizer(self.sizer)

        self.btns: List[buttons.GenButton] = []
        self._init_pathsetting_gui()
        self._init_btn_color()

        '''
            BUG 重要！！！必须先展开，以便让父容器感知到存在（不知道是不是版本BUG）
        '''
        self.collapsDirsPanel.Collapse(self.collapsDirsPanel.IsExpanded()) # 展开操作
        self.Layout()

    def _init_pathsetting_gui(self):
        '''路径设置GUI初始化'''
        self.collapsDirsPanel = PCP.PyCollapsiblePane(self, label="Django项目解析路径配置（扩展用）【点击显示】", agwStyle=wx.CP_NO_TLW_RESIZE)
        self.sizer.Add(self.collapsDirsPanel, 1, wx.EXPAND | wx.ALL, 0)
        collapsiblePanel = self.collapsDirsPanel.GetPane()
        
        '''
            网格布局
        '''
        flexGridSizer = wx.FlexGridSizer(cols=6, hgap=1, vgap=5)
        flexGridSizer.AddGrowableCol(1) # 第二列自适应

        '''
            App应用程序路径设置
        '''
        self.appLabel = wx.StaticText(collapsiblePanel, -1, label='App应用程序根路径集合')
        self.appInput = wx.TextCtrl(collapsiblePanel, -1, ';'.join(env.get_obj_rootpaths('apps')), style=wx.TE_READONLY)
        self.appBtnAddDir = buttons.GenButton(collapsiblePanel, -1, label='增', size=(29, -1))
        self.appBtnDeleteDir = buttons.GenButton(collapsiblePanel, -1, label='删', size=(29, -1))
        self.appBtnShowDir = buttons.GenButton(collapsiblePanel, -1, label='查', size=(29, -1))
        self.appBtnClearDir = buttons.GenButton(collapsiblePanel, -1, label='清空', size=(43, -1))

        flexGridSizer.Add(self.appLabel, 0, wx.ALIGN_RIGHT|wx.ALIGN_CENTER_VERTICAL)
        flexGridSizer.Add(self.appInput, 1, wx.EXPAND)
        flexGridSizer.Add(self.appBtnAddDir, 0, wx.EXPAND)
        flexGridSizer.Add(self.appBtnDeleteDir, 0, wx.EXPAND)
        flexGridSizer.Add(self.appBtnShowDir, 0, wx.EXPAND)
        flexGridSizer.Add(self.appBtnClearDir, 0, wx.EXPAND)

        '''
            Model模型路径设置
        '''
        self.modelLabel = wx.StaticText(collapsiblePanel, -1, label='Model模型根路径集合')
        self.modelInput = wx.TextCtrl(collapsiblePanel, -1, ';'.join(env.get_obj_rootpaths('models')), style=wx.TE_READONLY)
        self.modelBtnAddDir = buttons.GenButton(collapsiblePanel, -1, label='增', size=(29, -1))
        self.modelBtnDeleteDir = buttons.GenButton(collapsiblePanel, -1, label='删', size=(29, -1))
        self.modelBtnShowDir = buttons.GenButton(collapsiblePanel, -1, label='查', size=(29, -1))
        self.modelBtnClearDir = buttons.GenButton(collapsiblePanel, -1, label='清空', size=(43, -1))

        flexGridSizer.Add(self.modelLabel, 0, wx.ALIGN_RIGHT|wx.ALIGN_CENTER_VERTICAL)
        flexGridSizer.Add(self.modelInput, 1, wx.EXPAND)
        flexGridSizer.Add(self.modelBtnAddDir, 0, wx.EXPAND)
        flexGridSizer.Add(self.modelBtnDeleteDir, 0, wx.EXPAND)
        flexGridSizer.Add(self.modelBtnShowDir, 0, wx.EXPAND)
        flexGridSizer.Add(self.modelBtnClearDir, 0, wx.EXPAND)

        '''
            View视图路径设置
        '''
        self.viewLabel = wx.StaticText(collapsiblePanel, -1, label='View视图根路径集合')
        self.viewInput = wx.TextCtrl(collapsiblePanel, -1, ';'.join(env.get_obj_rootpaths('views')), style=wx.TE_READONLY)
        self.viewBtnAddDir = buttons.GenButton(collapsiblePanel, -1, label='增', size=(29, -1))
        self.viewBtnDeleteDir = buttons.GenButton(collapsiblePanel, -1, label='删', size=(29, -1))
        self.viewBtnShowDir = buttons.GenButton(collapsiblePanel, -1, label='查', size=(29, -1))
        self.viewBtnClearDir = buttons.GenButton(collapsiblePanel, -1, label='清空', size=(43, -1))

        flexGridSizer.Add(self.viewLabel, 0, wx.ALIGN_RIGHT|wx.ALIGN_CENTER_VERTICAL)
        flexGridSizer.Add(self.viewInput, 1, wx.EXPAND)
        flexGridSizer.Add(self.viewBtnAddDir, 0, wx.EXPAND)
        flexGridSizer.Add(self.viewBtnDeleteDir, 0, wx.EXPAND)
        flexGridSizer.Add(self.viewBtnShowDir, 0, wx.EXPAND)
        flexGridSizer.Add(self.viewBtnClearDir, 0, wx.EXPAND)

        '''
            Urls路由路径设置
        '''
        self.urlLabel = wx.StaticText(collapsiblePanel, -1, label='Url路由根路径集合')
        self.urlInput = wx.TextCtrl(collapsiblePanel, -1, ';'.join(env.get_obj_rootpaths('urls')), style=wx.TE_READONLY)
        self.urlBtnAddDir = buttons.GenButton(collapsiblePanel, -1, label='增', size=(29, -1))
        self.urlBtnDeleteDir = buttons.GenButton(collapsiblePanel, -1, label='删', size=(29, -1))
        self.urlBtnShowDir = buttons.GenButton(collapsiblePanel, -1, label='查', size=(29, -1))
        self.urlBtnClearDir = buttons.GenButton(collapsiblePanel, -1, label='清空', size=(43, -1))

        flexGridSizer.Add(self.urlLabel, 0, wx.ALIGN_RIGHT|wx.ALIGN_CENTER_VERTICAL)
        flexGridSizer.Add(self.urlInput, 1, wx.EXPAND)
        flexGridSizer.Add(self.urlBtnAddDir, 0, wx.EXPAND)
        flexGridSizer.Add(self.urlBtnDeleteDir, 0, wx.EXPAND)
        flexGridSizer.Add(self.urlBtnShowDir, 0, wx.EXPAND)
        flexGridSizer.Add(self.urlBtnClearDir, 0, wx.EXPAND)

        '''
            包装网格布局，添加进折叠布局中（重要！！！否则无法正常显示控件！！！）
        '''
        border = wx.BoxSizer(wx.HORIZONTAL)
        border.Add(flexGridSizer, 1, wx.EXPAND|wx.ALL, 5)
        collapsiblePanel.SetSizer(border)

        # 按钮控件统一管理
        self.btns.extend([
            self.appBtnAddDir, self.appBtnDeleteDir, self.appBtnShowDir, self.appBtnClearDir,
            self.modelBtnAddDir, self.modelBtnDeleteDir, self.modelBtnShowDir, self.modelBtnClearDir,
            self.viewBtnAddDir, self.viewBtnDeleteDir, self.viewBtnShowDir, self.viewBtnClearDir,
            self.urlBtnAddDir, self.urlBtnDeleteDir, self.urlBtnShowDir, self.urlBtnClearDir,
        ])

        # 注册监听事件
        self.Bind(wx.EVT_BUTTON, self.onBtnAddPath, self.appBtnAddDir)
        self.Bind(wx.EVT_BUTTON, self.onBtnAddPath, self.modelBtnAddDir)
        self.Bind(wx.EVT_BUTTON, self.onBtnAddPath, self.viewBtnAddDir)
        self.Bind(wx.EVT_BUTTON, self.onBtnAddPath, self.urlBtnAddDir)
        self.Bind(wx.EVT_BUTTON, self.onBtnDeletePath, self.appBtnDeleteDir)
        self.Bind(wx.EVT_BUTTON, self.onBtnDeletePath, self.modelBtnDeleteDir)
        self.Bind(wx.EVT_BUTTON, self.onBtnDeletePath, self.viewBtnDeleteDir)
        self.Bind(wx.EVT_BUTTON, self.onBtnDeletePath, self.urlBtnDeleteDir)
        self.Bind(wx.EVT_BUTTON, self.onBtnShowPath, self.appBtnShowDir)
        self.Bind(wx.EVT_BUTTON, self.onBtnShowPath, self.modelBtnShowDir)
        self.Bind(wx.EVT_BUTTON, self.onBtnShowPath, self.viewBtnShowDir)
        self.Bind(wx.EVT_BUTTON, self.onBtnShowPath, self.urlBtnShowDir)
        self.Bind(wx.EVT_BUTTON, self.onBtnClearPath, self.appBtnClearDir)
        self.Bind(wx.EVT_BUTTON, self.onBtnClearPath, self.modelBtnClearDir)
        self.Bind(wx.EVT_BUTTON, self.onBtnClearPath, self.viewBtnClearDir)
        self.Bind(wx.EVT_BUTTON, self.onBtnClearPath, self.urlBtnClearDir)
        # 折叠按钮监听
        self.Bind(wx.EVT_COLLAPSIBLEPANE_CHANGED, self.onDirsPaneChanged, self.collapsDirsPanel)

    def onBtnAddPath(self, evt):
        '''新增路径事件'''
        back_data = RichMsgDialog.showSelectFoldersDialog(self, info='追加')
        if back_data is not None:
            if evt.GetId() == self.appBtnAddDir.GetId():
                exits_dirs = env.get_obj_rootpaths('apps')
                if back_data not in exits_dirs:
                    env.add_rootpath_for_obj([back_data,], op_type='apps') # 及时存储（存储操作一定在之前）
                    temp = self.appInput.GetValue()
                    self.appInput.SetValue(f'{temp}{";" if temp else ""}{back_data}') # 及时展示
                else:
                    RichMsgDialog.showOkMsgDialog(self, '请勿重复添加', '警告')
            elif evt.GetId() == self.modelBtnAddDir.GetId():
                exits_dirs = env.get_obj_rootpaths('models')
                if back_data not in exits_dirs:
                    env.add_rootpath_for_obj([back_data,], op_type='models')
                    temp = self.modelInput.GetValue()
                    self.modelInput.SetValue(f'{temp}{";" if temp else ""}{back_data}')
                else:
                    RichMsgDialog.showOkMsgDialog(self, '请勿重复添加', '警告')
            elif evt.GetId() == self.viewBtnAddDir.GetId():
                exits_dirs = env.get_obj_rootpaths('views')
                if back_data not in exits_dirs:
                    env.add_rootpath_for_obj([back_data,], op_type='views')
                    temp = self.viewInput.GetValue()
                    self.viewInput.SetValue(f'{temp}{";" if temp else ""}{back_data}')
                else:
                    RichMsgDialog.showOkMsgDialog(self, '请勿重复添加', '警告')
            elif evt.GetId() == self.urlBtnAddDir.GetId():
                exits_dirs = env.get_obj_rootpaths('urls')
                if back_data not in exits_dirs:
                    env.add_rootpath_for_obj([back_data,], op_type='urls')
                    temp = self.urlInput.GetValue()
                    self.urlInput.SetValue(f'{temp}{";" if temp else ""}{back_data}')
                else:
                    RichMsgDialog.showOkMsgDialog(self, '请勿重复添加', '警告')

    def onBtnDeletePath(self, evt):
        '''删除路径事件'''
        if evt.GetId() == self.appBtnDeleteDir.GetId():
            temp = RichMsgDialog.showMultiChoiceDialog(self, '选择要删除的应用程序根路径', '删除根路径', env.get_obj_rootpaths('apps'))
            env.delete_operate_rootpath(paths=temp, op_type='apps')
            self.appInput.SetValue(';'.join(env.get_obj_rootpaths('apps')))
        elif evt.GetId() == self.modelBtnDeleteDir.GetId():
            temp = RichMsgDialog.showMultiChoiceDialog(self, '选择要删除的模型根路径', '删除根路径', env.get_obj_rootpaths('models'))
            env.delete_operate_rootpath(paths=temp, op_type='models')
            self.modelInput.SetValue(';'.join(env.get_obj_rootpaths('models')))
        elif evt.GetId() == self.viewBtnDeleteDir.GetId():
            temp = RichMsgDialog.showMultiChoiceDialog(self, '选择要删除的视图根路径', '删除根路径', env.get_obj_rootpaths('views'))
            env.delete_operate_rootpath(paths=temp, op_type='views')
            self.viewInput.SetValue(';'.join(env.get_obj_rootpaths('views')))
        elif evt.GetId() == self.urlBtnDeleteDir.GetId():
            temp = RichMsgDialog.showMultiChoiceDialog(self, '选择要删除的路由根路径', '删除根路径', env.get_obj_rootpaths('urls'))
            env.delete_operate_rootpath(paths=temp, op_type='urls')
            self.urlInput.SetValue(';'.join(env.get_obj_rootpaths('urls')))
        
    def onBtnShowPath(self, evt):
        '''查看路径事件'''
        msg, title = '', ''
        if evt.GetId() == self.appBtnShowDir.GetId():
            msg = '\n'.join(env.get_obj_rootpaths('apps'))
            title = '查看应用程序根路径集合'
        elif evt.GetId() == self.modelBtnShowDir.GetId():
            msg = '\n'.join(env.get_obj_rootpaths('models'))
            title = '查看模型根路径集合'
        elif evt.GetId() == self.viewBtnShowDir.GetId():
            msg = '\n'.join(env.get_obj_rootpaths('views'))
            title = '查看视图根路径集合'
        elif evt.GetId() == self.urlBtnShowDir.GetId():
            msg = '\n'.join(env.get_obj_rootpaths('urls'))
            title = '查看路由根路径集合'
        RichMsgDialog.showScrolledMsgDialog(self, msg, title)

    def onBtnClearPath(self, evt):
        '''清空根路径扩展集合'''
        if evt.GetId() == self.appBtnClearDir.GetId():
            if RichMsgDialog.showYesNoMsgDialog(self, '确认清空', '警告'):
                env.delete_operate_rootpath(clear=True, op_type='apps')
                self.appInput.SetValue('')
        elif evt.GetId() == self.modelBtnClearDir.GetId():
            if RichMsgDialog.showYesNoMsgDialog(self, '确认清空', '警告'):
                env.delete_operate_rootpath(clear=True, op_type='models')
                self.modelInput.SetValue('')
        elif evt.GetId() == self.viewBtnClearDir.GetId():
            if RichMsgDialog.showYesNoMsgDialog(self, '确认清空', '警告'):
                env.delete_operate_rootpath(clear=True, op_type='views')
                self.viewInput.SetValue('')
        elif evt.GetId() == self.urlBtnClearDir.GetId():
            if RichMsgDialog.showYesNoMsgDialog(self, '确认清空', '警告'):
                env.delete_operate_rootpath(clear=True, op_type='urls')
                self.urlInput.SetValue('')

    def _init_btn_color(self):
        '''初始化按钮前景色和背景色'''
        for _ in self.btns:
            _.SetBackgroundColour(CON_COLOR_MAIN)
            _.SetForegroundColour(CON_COLOR_PURE_WHITE)

    def onDirsPaneChanged(self, evt):
        '''根路径 折叠按钮监听'''
        self.Layout()
        if self.collapsDirsPanel.IsExpanded():
            self.collapsDirsPanel.SetLabel("Django项目解析路径配置（扩展用）【点击隐藏】")
        else:
            self.collapsDirsPanel.SetLabel("Django项目解析路径配置（扩展用）【点击显示】")
